package com.atguigu.gmall.list.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.dao.GoodsDao;
import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.vo.GoodsSearchResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/rpc/inner/es")
@RestController
public class GoodsRpcController {
    @Autowired
    GoodsService goodsService;

    /**
     * 上架商品,保存数据到ES
     * @param goods
     * @return
     */
    @PostMapping("/goods/save")
    public Result saveGoods(@RequestBody Goods goods){
        //TODO:goods null
        goodsService.saveGoods(goods);

        return Result.ok();
    }

    /**
     * 下架商品,从ES中删除数据
     * @param skuId
     * @return
     */
    @GetMapping("/goods/delete/{skuId}")
    public Result deleteGoods(@PathVariable("skuId") Long skuId){
        goodsService.deleteGoods(skuId);

        return Result.ok();
    }

    /**
     * 根据传递的参数,检索
     * @param param
     * @return
     */
    @PostMapping("/goods/search")
    public Result<GoodsSearchResultVo> searchGoods(@RequestBody SearchParam param,
                                                   HttpServletRequest request){

        //
        GoodsSearchResultVo vo=goodsService.searchGoods(param);

        return Result.ok(vo);
    }

}
